﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.Linq.Extensions;
using DHJ.Cost.Tasks;
using DHJ.Cost.Timing;
using DHJ.Cost.Workshops;
using System.Collections.Generic;
using System.Linq;
using Abp.Extensions;
using Microsoft.EntityFrameworkCore;

namespace DHJ.Cost.ManHours
{
    public class ManHourManager : DomainService, IManHourManager
    {
        private readonly IRepository<BatchManHour> _mhRepository;

        public ManHourManager(IRepository<BatchManHour> mhRepository)
        {
            _mhRepository = mhRepository;
        }

        public IQueryable<BatchManHour> ManHours => _mhRepository.GetAll();

        public List<BatchManHour> GetManHours(OccurrenceTime time, Workshop? workshop, string productId = null, string taskId = null, bool includingFrozen = false)
        {
            return ManHours
                .Include(mh => mh.Batch)
                .ThenInclude(b => b.Task)
                .Where(mh => mh.Time == time)
                .WhereIf(workshop.HasValue, mh => mh.Workshop == workshop.Value)
                .WhereIf(!productId.IsNullOrWhiteSpace(), mh => mh.Batch.Task.ProductId == productId)
                .WhereIf(!taskId.IsNullOrWhiteSpace(), mh => mh.Batch.Task.TaskNum == taskId)
                .WhereIf(!includingFrozen, mh => mh.Batch.AccountingState != AccountingState.Frozen)
                .ToList();
        }

        //public List<BatchManHour> GetAllManHours(OccurrenceTime time, Workshop? workshop)
        //{
        //    return GetManHours(time, workshop, null, null, false).ToList();
        //}

        //public List<BatchManHour> GetManHoursByModel(OccurrenceTime time, Workshop? workshop, string modelId)
        //{
        //    return GetManHours(time, workshop, modelId, null, false).ToList();
        //}

        //public List<BatchManHour> GetManHoursByTask(OccurrenceTime time, Workshop? workshop, string taskId)
        //{
        //    return GetManHours(time, workshop, null, taskId, false).ToList();
        //}
    }
}
